package jwlee.testnetwork;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Scanner;

public class NetTester {

	public static void main(String[] args) {
		//testHalfClose();
		 testInetAddress();
		// testURLcoder();
	}

	public static void testHalfClose() {
		try {
			new Thread("client") {
				public void run() {
					try {
						Socket s = new Socket("127.0.0.1", 30000);
						PrintStream ps = new PrintStream(s.getOutputStream());
						ps.println("hello server!");

						BufferedReader br = new BufferedReader(
								new InputStreamReader(s.getInputStream()));
						String content = null;
						// 不断读取Socket输入流中的内容，并将这些内容打印输出
						while ((content = br.readLine()) != null) {
							System.out.println("client:" + content);
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}.start();
			ServerSocket ss = new ServerSocket(30000);
			Socket socket = ss.accept();
			PrintStream ps = new PrintStream(socket.getOutputStream());
			ps.println("服务器的第一行数据");
			ps.println("服务器的第二行数据");
			// 关闭socket的输出流，表明输出数据已经结束
			socket.shutdownOutput();
			// 下面语句将输出false，表明socket还未关闭。
			System.out.println("server:" + socket.isClosed());
			Scanner scan = new Scanner(socket.getInputStream());
			if (scan.hasNextLine()) {
				System.out.println("server:" + scan.nextLine());
			}
			scan.close();
			socket.close();
			ss.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void testURLcoder() {
		try {
			// 将application/x-www-form-urlencoded字符串转换成普通字符串
			String keyWord = URLDecoder.decode("%E6%9D%8E%E5%88%9A+j2ee",
					"UTF-8");
			System.out.println(keyWord);
			// 将普通字符串转换成application/x-www-form-urlencoded字符串
			String urlStr = URLEncoder.encode("ROR敏捷开发最佳指南", "GBK");
			System.out.println(urlStr);
		} catch (UnsupportedEncodingException e) {
			System.err.println(e);
		}
	}

	private static void testInetAddress() {
		try {
			// 根据主机名来获取对应的InetAddress实例
			InetAddress ip = InetAddress.getByName("www.oneedu.cn");
			// 判断是否可达
			System.out.println("oneedu是否可达：" + ip.isReachable(2000));
			// 获取该InetAddress实例的IP字符串
			System.out.println(ip.getHostAddress());
		} catch (Exception e) {
			System.err.println(e);
		}

		try {
			// 根据原始IP地址来获取对应的InetAddress实例
			InetAddress local = InetAddress.getByAddress(new byte[] { 127, 0,
					0, 1 });
			System.out.println("本机是否可达：" + local.isReachable(2000));
			// 获取该InetAddress实例对应的全限定域名
			System.out.println(local.getCanonicalHostName());
			System.out.println(local.getHostAddress());
			System.out.println(local.getHostName());
		} catch (Exception e) {
			System.err.println(e);
		}
		
		try {
			InetAddress[] inetAdds = InetAddress.getAllByName(InetAddress
					.getLocalHost().getHostName());
			System.out.println(Arrays.asList(inetAdds));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}   
	}
}
